//
//  DMTiXianRecordViewController.m
//  DMGME
//
//  Created by MC on 15/10/10.
//  Copyright (c) 2015年 GYP. All rights reserved.
//

#import "DMTiXianRecordViewController.h"
#import "tiXianRecordCell.h"
#import "DMTixianModel.h"

@interface DMTiXianRecordViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>

@property (strong, nonatomic) NSMutableArray *fakeColors;
@property (strong, nonatomic) NSMutableArray *datas;
@property (strong, nonatomic) UITableView *tableView;
@property (nonatomic,strong)UILabel *tishiLabel;

@end

@implementation DMTiXianRecordViewController
- (NSMutableArray *)fakeColors
{
    if (!_fakeColors) {
        self.fakeColors = [NSMutableArray arrayWithObjects:@"成功",@"失败",@"成功",@"失败",@"成功", nil];
        
    }
    return _fakeColors;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"提现记录";
    [self AddLeftImageBtn:[UIImage imageNamed:@"fanhui_03 2"]target:self action:@selector(GoBack)];
    self.view.backgroundColor = [UIColor whiteColor];
    _datas = [NSMutableArray array];
    [self createTableView];
    [self tiXianDataRequest];
    [_tableView registerClass:[tiXianRecordCell class] forCellReuseIdentifier:@"cell"];
    _tishiLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, _tableView.frame.origin.x+20, KWidth-200, 20)];
    _tishiLabel.textAlignment = NSTextAlignmentCenter;
    _tishiLabel.font = [UIFont systemFontOfSize:12];
    _tishiLabel.textColor = [UIColor grayColor];
    _tishiLabel.text = @"目前暂无数据";
    _tishiLabel.hidden = YES;
    [_tableView addSubview:_tishiLabel];

    
}
-(void)createTableView
{
    _tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.view addSubview:_tableView];
}
#pragma mark TableViewDelegate/DataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _datas.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ident = @"cell";
    
    tiXianRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:ident forIndexPath:indexPath];
    
    DMTixianModel *model = _datas[indexPath.row];
    cell.model = model;
//    NSLog(@"model.status === %@",model.status);
    if (model.status.intValue == 0) {
        cell.againApply.hidden = NO;
    }
    if (model.status.intValue == 1)
    {
        cell.againApply.hidden = YES;
    }
    [cell.againApply addTarget:self action:@selector(againApplyClick:) forControlEvents:UIControlEventTouchUpInside];
    cell.againApply.tag = model.id.integerValue;
    
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(void)tiXianDataRequest
{
    NSMutableDictionary *weiChatDic = [NSMutableDictionary dictionary];
    [weiChatDic setValue:[AFDownManager MD5String:@"%^&*dongmangoug!@#$index"] forKey:@"token"];
    [weiChatDic setValue:kkUserDict[@"id"] forKey:@"agent_id"];
    [weiChatDic setValue:@"index" forKey:@"action"];
    [self StartLoading];
    _tishiLabel.hidden = YES;

    [AFDownManager POST:BindingWeiChat parameters:weiChatDic success:^(id responseObject) {
//              NSLog(@"提现记录==%@",responseObject);
        
        [self StopLoading];
        
        [_datas removeAllObjects];

        if ([responseObject[@"data"][@"listData"] count] != 0) {
            
        for (NSDictionary *dic in responseObject[@"data"][@"listData"]) {
           
            
            DMTixianModel *mode = [DMTixianModel modelWithDic:dic];
            
            [_datas addObject:mode];
            
            [_tableView reloadData];
        }
           
         }else
            {
                _tishiLabel.hidden = NO;
            }
        } failure:^(NSError *error) {
               [self StopLoading];
        [self showMsg:@"网络故障"];
//        NSLog(@"dataRequest error = %@",error);
        
    }];

}
-(void)againApplyClick:(UIButton *)sender
{
//    [self againTiXianDataRequest:[NSString stringWithFormat:@"%ld",(long)sender.tag]];
//}
//-(void)againTiXianDataRequest:(NSString *)strId
//{
    UIView* v=[sender superview];//UITableViewCellContentView
    UITableViewCell* cell=(UITableViewCell*)[v superview];//UITableViewCell
    NSIndexPath* indexPath= [_tableView indexPathForCell:cell];
    DMTixianModel *model = _datas[indexPath.row];
    NSMutableDictionary *weiChatDic = [NSMutableDictionary dictionary];
    [weiChatDic setValue:[AFDownManager MD5String:@"%^&*dongmangoug!@#$aplayAgain"] forKey:@"token"];
    [weiChatDic setValue:kkUserDict[@"id"] forKey:@"agent_id"];
    [weiChatDic setValue:[NSString stringWithFormat:@"%ld",(long)sender.tag] forKey:@"id"];
    [weiChatDic setValue:model.money forKey:@"money"];
    [weiChatDic setValue:@"aplayAgain" forKey:@"action"];
    [self StartLoading];
    [AFDownManager POST:BindingWeiChat parameters:weiChatDic success:^(id responseObject) {
       
//        NSLog(@"再次提现记录==%@",responseObject);
        [self StopLoading];
        
        
        if ([responseObject[@"data"] count] != 0) {
            
            
            UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"成功" message:@"成功提现" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alter show];
            
        }else
        {
            [self showMsg:responseObject[@"desc"]];
        }
    } failure:^(NSError *error) {
        [self StopLoading];
        [self showMsg:@"网络故障"];
//        NSLog(@"dataRequest error = %@",error);
        
    }];

}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self tiXianDataRequest];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
